#ifndef RBT_BYTES_INCLUDE
#define RBT_BYTES_INCLUDE

#include "typedef.h"


RETURNCODE E_BYTES_ALLOCATE   = -2401;
RETURNCODE E_BYTES_OUTOFRANGE = -2402;

class Cache
{
	byte*  m_bytes;
	uint32 m_byteSize;
protected:
	Cache( const Cache& bytes )
	{

	}
public:

	Cache()
	{
		m_bytes    = 0;
		m_byteSize = 0;
	}

	typedef byte    value_type;
	typedef byte*   iterator;
	typedef byte&   reference;
	typedef cbyte*  const_iterator;
	typedef uint32  difference_type;
	typedef uint32  distance_type;

	iterator begin()
	{
		return m_bytes;
	}

	iterator end()
	{
		return m_bytes + m_byteSize;
	}

	const_iterator begin() const
	{
		return m_bytes;
	}

	const_iterator end() const
	{
		return m_bytes + m_byteSize;
	}

	uint32 size() const
	{
		return m_byteSize;
	}

	bool empty() const
	{
		return (m_byteSize == 0);
	}



	operator cbyte* () const
	{
		return m_bytes;
	}

	operator byte*()
	{
		return m_bytes;
	}

	operator void*()
	{
		return (void*)m_bytes;
	}

	int32 GetSize( uint32& byteSize ) const
	{
		byteSize = m_byteSize;
		return R_SUCCEED;
	}

	int32 Initalize( const Cache& bytes )
	{
		return Initalize( bytes , bytes.GetSize() );
	}

	uint32 GetSize() const
	{
		return m_byteSize;
	}

	int32 SetSize( uint32& byteSize )
	{
		if( byteSize > m_byteSize )
			return E_BYTES_OUTOFRANGE;
		m_byteSize = byteSize;
		return R_SUCCEED;
	}

	int32 Initalize( cuint32 byteSize )
	{
		Uninitalize();

		m_bytes = (byte*)malloc( byteSize );
		if( m_bytes == 0 )
			return E_BYTES_ALLOCATE;
		m_byteSize = byteSize;
		return R_SUCCEED;
	}

	int32 Initalize( cbyte* bytes, cuint32 byteSize )
	{
		int32 code = Initalize( byteSize );

		if( code != R_SUCCEED )
		{
			Uninitalize();
			return code;
		}

		memcpy_s( *this , GetSize() , (void*)bytes , byteSize );

		return R_SUCCEED;
	}

	int32 Uninitalize()
	{
		if( m_bytes != 0 )
			free(m_bytes);
		m_byteSize = 0;
		m_bytes    = 0;
		return R_SUCCEED;
	}

	~Cache()
	{
		Uninitalize();
	}
};

typedef Cache cache;
typedef const Cache ccache;

#endif